//
//  ZGLPopSerialPassView.m
//  ZeegingLottery
//
//  Created by Alfa Li on 2018/6/15.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLPopSerialPassView.h"

@interface ZGLPopSerialPassView ()
{
    CGFloat contentViewH;
    NSInteger selectedCount;
}

/** 弹窗主内容view */
@property (nonatomic,strong) UIView *contentView;
/** 弹窗标题 */
@property (nonatomic,strong) UILabel *titleLable;
/** 左边取消按钮 */
@property (nonatomic,strong) UIButton *cancelBtn;
/** 右边确认按钮 */
@property (nonatomic,strong) UIButton *confirmBtn;

@property (nonatomic,strong) UIView *subContentView;

@property (nonatomic,strong)NSMutableArray *selectedBtnArray;      //选中串关方式

@property (strong, nonatomic)NSMutableArray *seletedStateArray;         //选中状态

@property (strong, nonatomic)NSMutableArray *ggTypeStringArrray;   //串关列表

@end

@implementation ZGLPopSerialPassView

- (void)setCount:(NSInteger)count
{
    _count = count;
    selectedCount = count;
    [self CreateButtonWithCount:count warp:3];
    
    
}

- (void)setInComeSelectedBtnStateArray:(NSMutableArray *)InComeSelectedBtnStateArray
{
    if (InComeSelectedBtnStateArray.count>0) {
        _InComeSelectedBtnStateArray = InComeSelectedBtnStateArray;

        [self.seletedStateArray removeAllObjects];
        self.seletedStateArray = InComeSelectedBtnStateArray;
        
        for (UIButton *button in self.subContentView.subviews) {
            
            if ([button isKindOfClass:[UIButton class]]) {
                
                NSInteger tag = button.tag;
                NSString *numStr = _seletedStateArray[tag];
                BOOL isSelected = [numStr integerValue];
                if (isSelected) {
                    button.layer.borderColor = RGBACOLOR(246, 63, 63, 1.0).CGColor;
                }else{
                    button.layer.borderColor = BorderColor.CGColor;
                }
                [button setSelected:isSelected];
                
                
            }
            
            
        }
        
    }

}

- (void)setInComeSelectedBtnArray:(NSMutableArray *)InComeSelectedBtnArray
{
    if (InComeSelectedBtnArray.count>0) {
        _InComeSelectedBtnArray = InComeSelectedBtnArray;
        
        [self.selectedBtnArray removeAllObjects];
        self.selectedBtnArray = InComeSelectedBtnArray;
        
        DLog(@"incomeSelectedBtnArray--%@",self.selectedBtnArray);
    }
    
    
    
}

//串关列表
- (NSMutableArray *)ggTypeStringArrray
{
    if (!_ggTypeStringArrray) {
        _ggTypeStringArrray = [NSMutableArray array];
    }
    return _ggTypeStringArrray;
}


- (NSMutableArray *)selectedBtnArray
{
    if (!_selectedBtnArray) {
        _selectedBtnArray = [NSMutableArray array];
        NSMutableArray *mtArr = [NSMutableArray array];
        for (int i = 1; i<selectedCount; i++) {

            //默认选择
            if (i+1 == selectedCount) {
                NSString *passStr = [NSString stringWithFormat:@"%d001",i+1];
                [mtArr addObject:passStr];
            }
        }
        _selectedBtnArray = mtArr;
    }
    return _selectedBtnArray;
}

//
- (NSMutableArray *)seletedStateArray
{
    if (!_seletedStateArray) {
        _seletedStateArray = [NSMutableArray array];
        NSMutableArray *mtArr = [NSMutableArray array];
        for (int i = 0; i < selectedCount; i++) {
            
            NSString *isSelected = @"0";
            if (i+1 == selectedCount-1) {
                isSelected = @"1";

            }
            [mtArr addObject:isSelected];
        }
        _seletedStateArray = mtArr;
        
    }
    
    return _seletedStateArray;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupContentView];
        
    }
    
    return self;
}

- (void)setupContentView
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = sRGBHexAlpha(0xC0C0C0, 0.5);
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
    
//    contentViewH = IS_IPHONE_X ? 180:150;
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView = contentView;
    [self addSubview:self.contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"串关方法";
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont systemFontOfSize:labelFontSize];
    self.titleLable = titleLabel;
    [_contentView addSubview:self.titleLable];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_contentView).offset(10);
        make.centerX.equalTo(_contentView);
        
    }];
    
    UIView *leftLine = [[UIView alloc]init];
    leftLine.backgroundColor = BorderColor;
    [_contentView addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_contentView).offset(20);
        make.right.equalTo(self.titleLable.mas_left).offset(-10);
        make.centerY.equalTo(self.titleLable.mas_centerY);
        make.height.equalTo(@(kBorderWidth));
        
    }];
    
    UIView *rightLine = [[UIView alloc]init];
    rightLine.backgroundColor = BorderColor;
    [_contentView addSubview:rightLine];
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLable.mas_right).offset(10);
        make.right.equalTo(_contentView).offset(-20);
        make.centerY.equalTo(leftLine);
        make.height.equalTo(@(kBorderWidth));
        
    }];
    
    
    UIView *subContentView = [[UIView alloc]init];
    self.subContentView = subContentView;
    [_contentView addSubview:self.subContentView];
    [_subContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLable.mas_bottom).offset(10);
        make.left.equalTo(self);
        make.right.equalTo(self);
        
    }];
    
    //
    CGFloat btnViewH = IS_IPHONE_X ? 70:40;
    UIView *cancelView = [[UIView alloc]init];
    cancelView.backgroundColor = [UIColor lightGrayColor];
    [_contentView addSubview:cancelView];
    [cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_subContentView.mas_bottom);
        make.left.equalTo(_contentView);
        make.width.equalTo(@(UI_SCREEN_WIDTH/2));
        make.bottom.equalTo(_contentView);
        make.height.equalTo(@(btnViewH));
        
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    cancelBtn.backgroundColor = [UIColor lightGrayColor];
    [cancelBtn addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn = cancelBtn;
    [cancelView addSubview:self.cancelBtn];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(cancelView);
        make.left.equalTo(cancelView);
        make.right.equalTo(cancelView);
//        make.bottom.equalTo(cancelView);
        make.height.equalTo(@(40));
        
    }];
    
    
    UIView *confirmView = [[UIView alloc]init];
    confirmView.backgroundColor = globalRed;
    [_contentView addSubview:confirmView];
    [confirmView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(cancelView);
        make.right.equalTo(_contentView);
        make.width.equalTo(@(UI_SCREEN_WIDTH/2));
        make.bottom.equalTo(_contentView);
        make.height.equalTo(@(btnViewH));
        
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    confirmBtn.backgroundColor = globalRed;
    [confirmBtn addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn = confirmBtn;
    [confirmView addSubview:self.confirmBtn];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(confirmView);
        make.right.equalTo(confirmView);
        make.left.equalTo(confirmView);
//        make.bottom.equalTo(confirmView);
        make.height.equalTo(@(40));
    }];
    
    
}


- (void)CreateButtonWithCount:(NSInteger )count warp:(NSInteger)warp
{
    
    for (int i = 0; i < count-1; i++) {
        
        NSString *ggTypeStr = [NSString stringWithFormat:@"%d001",i+2];
        [self.ggTypeStringArrray addObject:ggTypeStr];
        
        //设置默选状态
        NSString *numStr = self.seletedStateArray[i];
        BOOL isSelected = [numStr intValue];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"btn_mark_bottom_right_normal"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:globalRed forState:UIControlStateSelected];
        [button setTitleColor:RGBACOLOR(246, 63, 63, 1.0) forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:btnFontSize];
//        button.titleLabel.textAlignment = 1;
//        button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        button.tag = i;
        button.layer.borderWidth = kBorderWidth;
        button.layer.borderColor = BorderColor.CGColor;
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(selectBet:) forControlEvents:UIControlEventTouchUpInside];
        if (isSelected) {
            [button setSelected:YES];
            button.layer.borderColor = RGBACOLOR(246, 63, 63, 1.0).CGColor;
            //            button.layer.borderColor = [UIColor redColor].CGColor;
        }
        [button setTitle:[NSString stringWithFormat:@"%d串1",i+2] forState:UIControlStateNormal];
        

        [self.subContentView addSubview:button];
        DLog(@"subContentView.subviews--%@",self.subContentView.subviews);
        
    }
    
    [self.subContentView.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:30 fixedLineSpacing:10 fixedInteritemSpacing:20 warpCount:warp topSpacing:5 bottomSpacing:10 leadSpacing:20 tailSpacing:20];
    DLog(@"self.ggTypeStringArrray--%@",_ggTypeStringArrray);
    
}

#pragma mark - btnClick

//取消
- (void)cancelButtonClick
{
    DLog(@"取消");
    
    [self dismiss];
    
}

//确认
- (void)confirmButtonClick:(UIButton *)sender
{
    DLog(@"确认选择");
    DLog(@"self.selectedBtnArray--%@",self.selectedBtnArray);
    //返回串关列表
    if (self.SerialPassArrayBlock) {
        self.SerialPassArrayBlock(self.selectedBtnArray, self.seletedStateArray);
    }
    
    [self dismiss];
    
}

//串关类型
- (void)selectBet:(UIButton *)sender
{
    
    NSInteger tag = sender.tag;
    DLog(@"%ld串1",(long)tag);
    sender.selected = !sender.selected;
    
    NSString *passStr = self.ggTypeStringArrray[tag];
    
    DLog(@"self.selectedBtnArray--%@",self.selectedBtnArray);
    
    if (sender.isSelected) {
        [sender setBackgroundImage:[UIImage imageNamed:@"btn_mark_bottom_right_normal"] forState:UIControlStateSelected];
        sender.layer.borderColor = RGBACOLOR(246, 63, 63, 1.0).CGColor;
        
        [_seletedStateArray replaceObjectAtIndex:tag withObject:@"1"];
        [_selectedBtnArray addObject:passStr];
        
        
        
        
    }else{
        
        if (_selectedBtnArray.count <= 1) {
            
            DLog(@"请至少选择一个玩法");
            [sender setSelected:YES];
            
            [EasyTextView showText:@"请至少选择一个玩法" config:^EasyTextConfig *{
                
                EasyTextConfig *config = [EasyTextConfig shared];
                config.bgColor = sRGBHexAlpha(0xC0C0C0, 0.8) ;
                config.animationType = TextAnimationTypeFade;
                config.statusType = TextStatusTypeBottom ;
                
                return config;
            }];
            
            
            
        }else{
            [sender setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
            sender.layer.borderColor = BorderColor.CGColor;
            
            [_seletedStateArray replaceObjectAtIndex:tag withObject:@"0"];
            [_selectedBtnArray removeObject:passStr];
            
            
        }
        
        
    }
    
    DLog(@"selectedBtnArray--%@,_seletedStateArray--%@",self.selectedBtnArray,_seletedStateArray);

    
}

#pragma mark - show & dismiss
//弹出
-(void)showView
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    self.contentView.y = UI_SCREEN_HEIGHT;
    contentViewH = IS_IPHONE_X ? 180:150;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.contentView.y = UI_SCREEN_HEIGHT - contentViewH;
    }];
    
}

//移除
- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        
        self.contentView.y = UI_SCREEN_HEIGHT;
        
    } completion:^(BOOL finished) {
       
        [self removeFromSuperview];
        [_contentView removeFromSuperview];
        
    }];
    
}

- (void)dealloc
{
    DLog(@"全释放掉了，无内存泄漏");
    
}

@end
